import hashlib
import time
import urllib.parse

import requests
from box import Box
from web_tieba import get_tieba_likes, get_tbs

FAKE_VERSION = '12.12.0.2'
header = {
    'User-agent': f'bdtb for Android {FAKE_VERSION}',
    'Host': 'c.tieba.baidu.com',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept-Encoding': 'gzip',
    'Cookie': 'ka=open',
}


def sign_in_app(bduss, tbs, fid, kw):
    """

    Args:
        bduss:
        tbs: 页面随机值
        fid: 吧的ID
        kw: 吧名

    Returns: True or False

    """
    url = 'http://c.tieba.baidu.com/c/c/forum/sign'
    data = {
        "BDUSS": bduss,
        "tbs": tbs,
        "fid": fid,
        "kw": kw,
        "_client_type": 4,
        "_client_version": '11.8.8.0',
        "_phone_imei": "000000000000000",
        "model": "V2055A",
        "net_type": 1,
        "stErrorNums": 1,
        "stMethod": 1,
        "stMode": 1,
        "stSize": 320,
        "stTime": 117,
        "stTimesNum": 1,
        "timestamp": int(round(time.time() * 1000))
    }

    kvs = []
    for k, v in sorted(data.items()):
        kvs.append(f'{k}={v}')
    data_str = '&'.join(kvs)
    sign_str = ''.join(kvs) + "tiebaclient!!!"
    print(sign_str)
    sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()
    data_str += f'&sign={sign}'
    print(data_str)

    resp = requests.post(url, data=urllib.parse.quote(data_str), headers=header)
    if resp.status_code == 200:
        print(resp.json())


def t2():
    bduss = 'DJIM2tzdnZ5WGpwbU9ySWFiWjRndGpVVFpwT2RyNWFKcFdKclI2OXMtYmZkSlJoRVFBQUFBJCQAAAAAAAAAAAEAAAC2A-~E1-66w9LgysfX7rCuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN~nbGHf52xhe'
    kw = 'android开发'
    fid = '1957107'
    tbs = get_tbs(kw)
    print(tbs)

    sign_in_app(bduss, tbs, fid, kw)


d1 = 'BDUSS=FhCSFQ4cW5uMnRuNkpnclpCam01eHhGMFd0aFVRQjRTaEhFbWhyTDgteWlYeVpoSUFBQUFBJCQAAAAAAAAAAAEAAAC2A-%7EE1-66w9LgysfX7rCuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKLS%7EmCi0v5gM&_client_id=wappc_1634370510748_578&_client_type=2&_client_version=12.12.0.2&_phone_imei=000000000000000&active_timestamp=1634564354923&c3_aid=A00-BSQ5NUVNKL2CQ3V6UYFPSXLVNLQ2L24N-Y735YPM4&cmode=1&cuid=baidutiebaapp684ec5f6-aad4-413a-876f-efaf73e8fd21&cuid_galaxy2=8EE8A3FAC109AE29C425B2A3DE0F4AEA%7CVTLEEFNAJ&cuid_gid=&event_day=20211019&fid=59099&first_install_time=1631704105070&from=1014610n&is_teenager=0&kw=%E6%9D%8E%E6%AF%85&last_update_time=1634370493857&location=104.06850373589758%2C30.539413632587703&model=V2055A&net_type=1&sign=9E81DFDF0344D6829EB3D2873D1DA7BB&sign_from=frs&stErrorNums=1&stMethod=1&stMode=1&stSize=797&stTime=200&stTimesNum=1&stoken=56d990653054412cdb72d834c417f0bbaed26a5a7d1473da09c1229004d48de0&tbs=92dd340eda57d8921634631877&timestamp=1634631890629&z_id=46HPtBkG8ZIMLRvRDlG1UjNLruiYzZwKxpn7lzUKn9Jni2jozh5FXZNpSVIAx29Rzldu7UbwUWK6d0VW96_A_Kg'

d2 = 'BDUSS=FhCSFQ4cW5uMnRuNkpnclpCam01eHhGMFd0aFVRQjRTaEhFbWhyTDgteWlYeVpoSUFBQUFBJCQAAAAAAAAAAAEAAAC2A-%7EE1-66w9LgysfX7rCuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKLS%7EmCi0v5gM&_client_id=wappc_1634370510748_578&_client_type=2&_client_version=12.12.0.2&_phone_imei=000000000000000&active_timestamp=1634564354923&c3_aid=A00-BSQ5NUVNKL2CQ3V6UYFPSXLVNLQ2L24N-Y735YPM4&cmode=1&cuid=baidutiebaapp684ec5f6-aad4-413a-876f-efaf73e8fd21&cuid_galaxy2=8EE8A3FAC109AE29C425B2A3DE0F4AEA%7CVTLEEFNAJ&cuid_gid=&event_day=20211019&fid=55963&first_install_time=1631704105070&from=1014610n&is_teenager=0&kw=%E4%B8%AD%E5%9B%BD%E7%9F%BF%E4%B8%9A%E5%A4%A7%E5%AD%A6&last_update_time=1634370493857&location=104.06850373589758%2C30.539413632587703&model=V2055A&net_type=1&sign=4CB2293FC6AC684612D249282EB767F7&sign_from=frs&stErrorNums=0&stoken=56d990653054412cdb72d834c417f0bbaed26a5a7d1473da09c1229004d48de0&tbs=ca9feb7e6c3a7b151634628104&timestamp=1634628252055&z_id=46HPtBkG8ZIMLRvRDlG1UjNLruiYzZwKxpn7lzUKn9JnjG2hXQGJ9dW3QG1Hw47T1XttMccO9b3tFPOlaokjEAQ'


def my_parse(data):
    my_map = {}
    words = data.split('&')
    for word in words:
        t = word.split('=')
        k = t[0]
        v = t[1]
        my_map[k] = v
    my_box = Box(my_map)
    my_box.to_json('d2.json')


def test_md5():
    ks = {
        "BDUSS": "FhCSFQ4cW5uMnRuNkpnclpCam01eHhGMFd0aFVRQjRTaEhFbWhyTDgteWlYeVpoSUFBQUFBJCQAAAAAAAAAAAEAAAC2A-%7EE1-66w9LgysfX7rCuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKLS%7EmCi0v5gM",
        "tbs": "92dd340eda57d8921634631877",
        "fid": "59099",
        "kw": "%E6%9D%8E%E6%AF%85",
        "_client_id": "wappc_1634370510748_578",
        "_client_type": "2",
        "_client_version": "12.12.0.2",
        "_phone_imei": "000000000000000",
        "model": "V2055A",
        "net_type": "1",
        "stErrorNums": "1",
        "stMethod": "1",
        "stMode": "1",
        "stSize": "797",
        "stTime": "200",
        "stTimesNum": "1",
        "timestamp": "1634631890629",
    }
    kvs = []
    for k, v in sorted(ks.items()):
        kvs.append(f'{k}={v}')
    # data_str = '&'.join(kvs)
    sign_str = ''.join(kvs) + 'tiebaclient!!!'
    print(sign_str)
    sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()
    print(sign)


if __name__ == '__main__':
    my_parse(d2)
    t2()
    test_md5()
